//
//  XHRAddMedicalCell.m
//  快医
//
//  Created by 胥鸿儒 on 16/7/7.
//
//

#import "XHRAddMedicalCell.h"
@interface XHRAddMedicalCell()<UITextViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
/**疾病描述标题label*/
@property(nonatomic,weak)UILabel *illDescribeLabel;
/**描述内容的textView*/
@property(nonatomic,weak)UITextView *describeTextView;
/**病历照片标题lable*/
@property(nonatomic,weak)UILabel *iconTitleLabel;
/**iconButton*/
@property(nonatomic,weak)UIButton *iconButton;

/**配图*/
@property(nonatomic,strong)UIImage *icon;
@end

@implementation XHRAddMedicalCell


+ (instancetype)addMedicalCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
    static NSString *const addMedicalRecordTopID = @"addMedicalRecordTopID";
    static NSString *const addMedicalRecordBottomID = @"addMedicalRecordBottomID";
    
    XHRAddMedicalCell *cell = nil;
    if (indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:addMedicalRecordTopID];
        if (!cell) {
            cell = [[self alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:addMedicalRecordTopID];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        if (indexPath.row == 0)
            cell.textLabel.text = @"疾病类型:";
        else if (indexPath.row == 1)
            cell.textLabel.text = @"疾病细分:";
    }
    else
    {
        cell = [tableView dequeueReusableCellWithIdentifier:addMedicalRecordBottomID];
        if (!cell) {
            cell = [[self alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:addMedicalRecordBottomID];
        }
        [cell setupChildWithIndexPath:indexPath];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark -
#pragma mark - 初始化子控件
- (void)setupChildWithIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        [self.illDescribeLabel makeConstraints:^(MASConstraintMaker *make) {
            make.leading.top.offset(XHRMedicalRecordMargin * 1.5);
        }];
        [self.describeTextView makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.illDescribeLabel);
            make.trailing.offset(-XHRMedicalRecordMargin);
            make.top.equalTo(self.illDescribeLabel.bottom).offset(XHRMedicalRecordMargin * 1.5);
            make.bottom.offset(-XHRMedicalRecordMargin * 1.5);
        }];
    }
    else if (indexPath.row == 1) {
        [self.iconTitleLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.offset(XHRMedicalRecordMargin * 1.5);
        }];
        [self.iconButton makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.iconTitleLabel.bottom).offset(2 * XHRMedicalRecordMargin);
            make.leading.offset(XHRScreenWidth * 0.11);
            make.height.width.equalTo(XHRScreenWidth * 0.24);
        }];
    }
}
#pragma mark -
#pragma mark - textView delegate
- (void)textViewDidChange:(UITextView *)textView
{
    if (self.addMedicalCellBlock) {
        self.addMedicalCellBlock(textView.text,self.icon);
    }
}
#pragma mark -
#pragma mark - 监听按钮的点击
- (void)didClickAddIconButton:(UIButton *)sender
{
    [self setupAlertController];
    
    if (self.addMedicalCellBlock) {
        self.addMedicalCellBlock(self.describeTextView.text,self.icon);
    }
}
#pragma mark -
#pragma mark - 设置弹出提醒
- (void)setupAlertController
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请选择图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *actionAlbum = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self getImageFromAlbum];
        
    }];
    UIAlertAction *actionCamera = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    
    [alertController addAction:actionAlbum];
    [alertController addAction:actionCamera];
    [XHRKeyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}
#pragma mark -
#pragma mark - 打开相册获取图片
- (void)getImageFromAlbum
{
    //打开相片
    UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
    pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    pickerController.delegate = self;
    [XHRKeyWindow.rootViewController presentViewController:pickerController animated:YES completion:nil];
}
#pragma mark -
#pragma mark - imagePickerController delegate
//打开相册成功后调用的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    [self.iconButton setImage:image forState:UIControlStateNormal];
    
    self.icon = image;
    [picker dismissViewControllerAnimated:YES completion:^{
        if (self.addMedicalCellBlock) {
            self.addMedicalCellBlock(self.describeTextView.text,self.icon);
        }
    }];
}

#pragma mark -
#pragma mark - 懒加载控件
- (UILabel *)illDescribeLabel
{
    if (!_illDescribeLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text = @"病症描述:";
        label.font = [UIFont systemFontOfSize:17];
        label.textColor = [UIColor blackColor];
        [self.contentView addSubview:label];
        _illDescribeLabel = label;
    }
    return _illDescribeLabel;
}
- (UITextView *)describeTextView
{
    if (!_describeTextView)
    {
        UITextView *textView = [[UITextView alloc]init];
        textView.backgroundColor = XHRColorWithRGB(166, 166, 166);
        textView.delegate = self;
        [self.contentView addSubview:textView];
        _describeTextView = textView;
    }
    return _describeTextView;
}
- (UILabel *)iconTitleLabel
{
    if (!_iconTitleLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text = @"病历照片:";
        label.textColor = [UIColor blackColor];
        [self.contentView addSubview:label];
        _iconTitleLabel = label;
    }
    return _iconTitleLabel;
}
- (UIButton *)iconButton
{
    if (!_iconButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:@"compose_pic_add_highlighted"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(didClickAddIconButton:) forControlEvents:UIControlEventTouchUpInside];
        button.adjustsImageWhenHighlighted = NO;
        [self.contentView addSubview:button];
        _iconButton = button;
    }
    return _iconButton;
}
@end
